import { QueryFields, QueryJoin } from '@dataui/crud-request';

export class JoinMapUtils {
  private joins = new Map<string, QueryJoin>();
  getJoins(): QueryJoin[] {
    return Array.from(this.joins.values());
  }
  isJoin(field: string | string[]): boolean {
    if (Array.isArray(field)) field = field.join('.');
    return this.joins.has(field);
  }
  setJoin(field: string | string[], value?: QueryJoin) {
    if (Array.isArray(field)) field = field.join('.');
    this.joins.set(field, value || { field });
  }
  setJoinSelect(field: string | string[], select: QueryFields) {
    if (Array.isArray(field)) field = field.join('.');
    const join = this.joins.get(field);
    if (join) join.select = select;
  }
}
